package edu.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import edu.domain.Orders;
import edu.domain.OrdersItem;
import edu.dto.OrdersItemDto;
import edu.service.OrdersItemService;
import edu.service.OrdersService;
import edu.service.UserService;
import edu.support.base.BasePage;
import edu.support.base.controller.AdminBaseController;
import edu.support.mybatis.MybatisWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/admin/orders")
public class OrdersAdminController extends AdminBaseController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrdersItemService ordersItemService;

    /**
     * 列表
     *
     * @param model
     * @param orders
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Orders orders, BasePage rowPage) {
        QueryWrapper<Orders> wrapper = new MybatisWrapper<Orders>()
                .like("username", orders.getUsername())
                .orderByDesc("create_time");
        PageInfo<Orders> pageInfo = ordersService.page(rowPage, wrapper);

        for (Orders item : pageInfo.getList()) {
            QueryWrapper<OrdersItem> queryWrapper = new QueryWrapper<OrdersItem>()
                    .eq("orders_id", item.getId());
            List<OrdersItemDto> itemDtoList = ordersItemService.selectDto(queryWrapper);
            item.setItemList(itemDtoList);
        }

        setModelAttribute(model, pageInfo);
        return "admin/orders/list";
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    public String del(@PathVariable Long id, RedirectAttributes attributes) {
        ordersService.removeById(id);
        return redirect("/admin/orders/list", "删除成功", attributes);
    }


    /**
     * 详情
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("detail/{id}")
    public String detail(@PathVariable String id, Model model) {
        Orders orders = ordersService.getById(id);
        if (null == orders) {
            orders = new Orders();
        }
        model.addAttribute("orders", orders);
        return "admin/orders/detail";
    }

    /**
     * 更新
     *
     * @return
     */
    @RequestMapping("update")
    public String update(Orders orders, RedirectAttributes attributes) {
        ordersService.saveOrUpdate(orders);
        return redirect("/admin/orders/list", "操作成功", attributes);
    }

    /**
     * 发货
     *
     * @param id
     * @param attributes
     * @return
     */
    @GetMapping("send/{id}")
    public String send(@PathVariable String id, RedirectAttributes attributes) {
        Orders orders = ordersService.getById(id);
        orders.setStatus("已发货");
        ordersService.updateById(orders);
        return refresh("发货成功", attributes);
    }

}